# -*- coding:utf-8 -*-

import requests

# 实例
"""
'''
调用get方法,得到一个response对象,输出response类型,状态码,响应体类型,内容及cookies.  返回类型是requests.models.Response,响应体类型:str,cookies类型:RequestsCookieJar
'''
r = requests.get('http://www.baidu.com/')
print(type(r))
print(r.status_code)
print(type(r.text))
print(r.text)
print(r.cookies)

"""

# GET请求
"""
r = requests.get('http://httpbin.org/get')
print(r.text)
# 返回结果包含请求头,url,ip等信息.

# 构造请求链接,添加参数
data = {
    'name': 'yuan',
    'age': 25
}

r = requests.get("http://httpbin.org/get",params=data)
print(r.text)
#返回结果中,请求链接已被构造成: "http://httpbin.org/get?name=yuan&age=25"


'''
直接解析返回结果,得到字典格式,调用json方法

'''
r = requests.get("http://httpbin.org/get")
print(type(r.text))
print(r.json())
print(type(r.json()))
"""


#  抓取网页
#  加入headers信息,包含了User-Agent浏览器标识信息.

"""
import requests,re

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
}
r = requests.get("https://www.zhihu.com/explore",headers=headers)

pattern = re.compile('explore-feed.*?question_link.*?>(.*?)</a>',re.S)
titles = re.findall(pattern,r.text)
print(titles)
"""


# 抓取二进制数据
# 图片,音频,视频这些文件都是由二进制码组成的.

"""
import requests

r = requests.get("https://github.com/favicon.ico")
# print(r.text)  
# print(r.content)
# 打印了Response对象的两个属性: text和content
'''
text返回为乱码,content返回是bytes类型的数据.
'''

with open('favicon.ico','wb') as f:
# open方法,参数: 文件名称,以二进制写形式打开,向文件写入二进制数据
    f.write(r.content)
# 音频和视频文件也可以用这种方法获取.

"""


# POST请求
"""
import requests

data = {'name': 'yuan','age': '22'}
r = requests.post("http://httpbin.org/post",data=data)  
print(r.text)
# 获得返回结果,form部分就是提交的数据.
"""

# 状态码判断请求是否成功

"""
import requests 

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
}
r = requests.get('http://www.jianshu.com',headers=headers)
# print(type(r.status_code),r.status_code)  # 获取状态码
# print(type(r.headers),r.headers)  # 获取响应头
# print(type(r.cookies),r.cookies) # 获取Cookies
# print(type(r.url),r.url)  # 获取url
# print(type(r.history),r.history)  # 获取请求历史

# headers属性结果为requests.structures.CaseInsensitiveDict类型.
# cookies属性得到结果为:requests.cookies.RequestsCookieJar类型.


# exit() if not r.status_code == requests.codes.ok else print('Request successfully')

b = requests.get('http://www.jianshu.com')
exit() if not b.status_code == requests.codes.ok else print('Request successfully')
"""


# 高级用法

"""
# # 1. 文件上传
import requests

files = {'file': open('favicon.ico','rb')}
r = requests.post("http://httpbin.org/post",files=files)
print(r.text)

## cookies
r = requests.get("https://www.baidu.com")
print(r.cookies)
'''
首先调用cookies属性获取cookies, 类型为:RequestsCookieJar. 然后用items方法将其转化为元组组成的列表,遍历输出每一个Cookie的名称和值,实现cookie的遍历解析.
'''

for key,value in r.cookies.items():
    print(key + '=' + value)


## 用cookie 来维持登录状态,先登录51cto,再将Headers中的Cookie内容复制下来
headers = {
    'Cookie': 'acw_tc=3ccdc15d15447644146834672e6f6394ebbe4b4357e9255a2ef63d7f5e3a6e; _ourplusFirstTime=118-12-14-13-13-35; www51cto=59A92EE94F20F197ED1C76D014451898RIqs;showMsgOver=1; gr_user_id=0d9b1dbb-9ddd-421f-906b-c46d80638c25; 51ctologToken=3af36a121804c4b0c4a238414f4e8e67; blog_activity_blog1=1; yd_cookie=788c459d-f05d-41cabfb93bf1a29254c52d6debe3e28e0c29; gr_session_id_8c51975c40edfb67=fc8bd1b2-3277-4985-b7ca-24dc26ac5376; Hm_lvt_2283d46608159c3b39fc9f1178809c21=1545538492,1545538737,1545982506,1545986883; gr_session_id_8c51975c40edfb67_fc8bd1b2-3277-4985-b7ca-24dc26ac5376=true; _51ctologStr=data%3D%257Bvisitorid%3A%25223af36a121804c4b0c4a238414f4e8e67%2522%2CuserAagent%3A%2522Mozilla/5.0%2520%28X11%3B%2520Linux%2520x86_64%29%2520AppleWebKit/537.36%2520%28KHTML%2520%2520like%2520Gecko%29%2520Chrome/65.0.3325.146%2520Safari/537.36%2522%2Ctoken%3A%25223af36a121804c4b0c4a238414f4e8e67%2522%2Cuid%3A%2522%2522%2Cuuid%3A%2522531274e2-1ddb-c3c1-t6a5-o9ccf2a61bfa%2522%2Ctype%3A%2522close%2522%2Cdom%3A%2522%2522%2CdomId%3A%2522%2522%2CdomInnerTxt%3A%2522%2522%2Cprice%3A%2522%2522%2Cstudents_count%3A%2522%2522%2Cfavourite%3A%2522%2522%2Cvote%3A%2522%2522%2Cscrolling%3A%25220%2525%2522%2Cscreensize%3A%25221366X768%2522%2Curl%3A%2522http%25253A%25252F%25252Fhome.51cto.com%25252Findex%25252F%25253Freback%25253Dhttp%2525253A%2525252F%2525252Fblog.51cto.com%2525252Fuser%2525252Flogin-success%2525253Fsign%2525253D4638UVIEBQkEA1QEBVFSAANXAVMEAFEBUQZQXFRbEUZECUlOAFhaXhoGVVdBWh4BDVweW14bFFgIXlpdUAVWCg%25252526client%2525253Dweb%2522%2Cref%3A%2522http%25253A%25252F%25252Fblog.51cto.com%25252Fliyuanjie%2522%2Cfrom%3A%2522home%2522%2Cduration%3A%25229270%2522%2Ctime%3A%25221545986899472%2522%257D; pub_sauth1=AgxcCgIWXxdXUUBUDTBXBgwEA1JWBzwAV1cCXQ1SWg1Y; pub_sauth2=2626fe39ac48123cfde9aee89aca428d; pub_cookietime=0; PHPSESSID=8mcq7ujm6d1vj8t9ju77vi6n7s; _identity=e931015e4a6e0ba8a149f84da32138c5add1446be284515c1dc73c2cbe02fe4aa%3A2%3A%7Bi%3A0%3Bs%3A9%3A%22_identity%22%3Bi%3A1%3Bs%3A21%3A%22%5B12877605%2Ctrue%2C43200%5D%22%3B%7D; Hm_lvt_110fc9b2e1cae4d110b7959ee4f27e3b=1545987216; UM_distinctid=167f404d4c02-00794bf0151e6e-3b70065b-100200-167f404d4c1774; Hm_lpvt_110fc9b2e1cae4d110b7959ee4f27e3b=1545987231; _ourplusReturnCount=24; _ourplusReturnTime=118-12-28-16-54-0; Hm_lpvt_2283d46608159c3b39fc9f1178809c21=1545987241; WEB_USER_TIME=%7B%22num%22%3A1%2C%22uid%22%3A%2212877605%22%2C%22startTime%22%3A%222018-12-28T08%3A54%3A00.909Z%22%7D',
    'Host': 'blog.51cto.com',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
}
r = requests.get('http://blog.51cto.com/liyuanjie',headers=headers)
print(r.text)
print(r.status_code)
print(r.headers)

with open('blog.html','w') as f:
    f.write(r.text)
"""

## 3. 会话维持
"""
import requests 

requests.get('http://httpbin.org/cookies/set/number/123')
# 设置cookie,名称和内容

r = requests.get('http://httpbin.org/cookies')  # 获取当前cookies
print(r.text)
# 返回结果为 不能获取到设置的Cookies


# 使用Session
s = requests.Session()
s.get('http://httpbin.org/cookies/set/number/123')
r = s.get('http://httpbin.org/cookies')
print(r.text)
"""

# SSL证书验证
"""
import requests,logging
from requests.packages import urllib3

urllib3.disable_warnings()  # 忽略警告
# 或者:
logging.captureWarnings(True)  # 捕获警告到日志的方式忽略警告


response = requests.get('https://www.cuiqingcai.com',verify=False)
print(response.status_code)
"""

# 身份验证
"""
import requests
from requests.auth import HTTPBasicAuth
r = requests.get(url,auth=HTTPBasicAuth('username','password'))
"""


#  各个参数都可以通过Prepared Request来表示.
"""
from requests import Request,Session

url = 'http://httpbin.org/post'
data = {
    'name': 'yuan'
}

headers = {
    'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
}
'''
用url,data,headers参数构造一个Request对象,再调用Session的prepare_request方法将其转换为一个Prepared Request对象,最后调用send方法发送.
'''

s = Session()
req = Request('POST',url,data=data,headers=headers)
prepped = s.prepare_request(req)
r = s.send(prepped)
print(r.text)

"""

